package com.cqie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqie.dto.entrepot.EntrepotDrugInfoListDto;
import com.cqie.dto.medical.AddMedicalDrugInfoDto;
import com.cqie.dto.medical.UpdateMedicalDrugInfoDto;
import com.cqie.entity.MedicalDrugInfo;
import com.cqie.exception.SpecialException;
import com.cqie.mapper.medical.MedicalDrugInfoMapper;
import com.cqie.service.IMedicalDrugInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.util.RedisUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import static com.cqie.constant.FileUploadConstant.FILE_UPLOAD_PATH;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 不吃海鲜
 * @since 2024-12-04
 */
@Service
public class MedicalDrugInfoServiceImpl extends ServiceImpl<MedicalDrugInfoMapper, MedicalDrugInfo> implements IMedicalDrugInfoService {

    @Resource
    private MedicalDrugInfoMapper medicalDrugInfoMapper;

    private final RedisUtils redisUtils;

    public MedicalDrugInfoServiceImpl(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    /**
     * 新增药品信息
     *
     * @param addMedicalDrugInfoDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveMedicalDrugInfo(AddMedicalDrugInfoDto addMedicalDrugInfoDto) throws SpecialException {

        if (ObjectUtils.isEmpty(addMedicalDrugInfoDto.getDrugName())) {
            throw new SpecialException("药品名称不能为空");
        }


        // 判断药品名称 (品牌) 是否重复
        var data = medicalDrugInfoMapper.getDrugInfoByDrugAndBrand(addMedicalDrugInfoDto);

        if (!ObjectUtils.isEmpty(data)) {
            throw new SpecialException("药品信息不能重复");
        }

        return medicalDrugInfoMapper.saveMedicalDrugInfo(addMedicalDrugInfoDto);
    }

    /**
     * 查看药品信息List
     *
     * @param likeQuery 模糊查询
     * @param pageSize  每页显示条数
     * @param pageIndex 当前页数
     * @return
     */
    @Override
    public List<MedicalDrugInfo> getMedicalDrugInfoList(String likeQuery, String drugType, Integer pageSize,
                                                        Integer pageIndex) {
        // 分页
        Page<MedicalDrugInfo> page = new Page();
        page.setCurrent(pageIndex);
        page.setSize(pageSize);

        LambdaQueryWrapper<MedicalDrugInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MedicalDrugInfo::getRmSign, "0");

        if (!ObjectUtils.isEmpty(drugType)) {
            queryWrapper.eq(MedicalDrugInfo::getDrugType, drugType);
        }

        if (!ObjectUtils.isEmpty(likeQuery)) {
            queryWrapper
                    .and(wrapper -> wrapper
                            .like(MedicalDrugInfo::getDrugName, likeQuery)
                            .or()
                            .like(MedicalDrugInfo::getDrugCode, likeQuery));
        }


        IPage<MedicalDrugInfo> result = medicalDrugInfoMapper.selectPage(page, queryWrapper);
        return result.getRecords();
    }

    /**
     * 删除药品信息
     *
     * @param drugId
     * @return
     */
    @Override
    public Boolean deleteMedicalDrugInfo(Integer drugId) {
        return medicalDrugInfoMapper.deleteMedicalDrugInfo(drugId);
    }

    /**
     * 根据药品编号模糊查询
     *
     * @param drugCode 药品编号
     * @return
     */
    @Override
    public List<AddMedicalDrugInfoDto> getMedicalDrugInfoByDrugCode(String drugCode) {
        return medicalDrugInfoMapper.getMedicalDrugInfoByDrugCode(drugCode);
    }


    /**
     * 根据药品ID集合查询药品信息
     *
     * @param drugIdList
     * @return
     */
    @Override
    public List<EntrepotDrugInfoListDto> getMedicalDrugInfoByDrugIdList(List<Integer> drugIdList, Integer pageSize,
                                                                        Integer pageIndex) {
        if (ObjectUtils.isEmpty(drugIdList)) {
            return new ArrayList<>();
        }
        pageIndex = (pageIndex - 1) * pageSize;
        return medicalDrugInfoMapper.getMedicalDrugInfoByDrugIdList(drugIdList, pageSize, pageIndex);
    }

    /**
     * 根据id 获取药品信息
     *
     * @param drugId
     * @return
     */
    @Override
    public AddMedicalDrugInfoDto getMedicalDrugInfoByDrugId(Integer drugId) throws SpecialException {
        if (ObjectUtils.isEmpty(drugId)) {
            throw new SpecialException("药品id不能为空");
        }
        return medicalDrugInfoMapper.getMedicalDrugInfoByDrugId(drugId);
    }

//    /**
//     * 上传药品图片
//     * @param file
//     * @param redirectAttributes
//     * @return
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public Boolean uploadMedicalDrugInfoImg(MultipartFile file, RedirectAttributes redirectAttributes) {
//        if (file.isEmpty()) {
//            return false;
//        }
//
//        try {
//            // 获取文件名
//            String fileName = file.getOriginalFilename();
//            // 定义上传路径
//            Path uploadPath = Paths.get(FILE_UPLOAD_PATH + fileName);
//            // 保存文件
//            Files.copy(file.getInputStream(), uploadPath);
//
//            redirectAttributes.addFlashAttribute("message", "文件上传成功： " + fileName);
//
//            // 更新数据库
//            medicalDrugInfoMapper.updateMedicalDrugInfoImg(FILE_UPLOAD_PATH + fileName);
//            return true;
//        } catch (IOException e) {
//            e.printStackTrace();
//            return false;
//        }
//    }

    /**
     * 更新药品图片
     * @param drugInfo
     * @return
     */
    @Override
    public Boolean updateDrugPhoto(MedicalDrugInfo drugInfo) throws SpecialException {
        // 检查药品是否存在
        MedicalDrugInfo existDrug = medicalDrugInfoMapper.selectById(drugInfo.getDrugId());
        if (existDrug == null) {
            throw new SpecialException("药品不存在");
        }

        // 更新药品图片

        return medicalDrugInfoMapper.updateDrugPhoto(drugInfo);

    }

    /**
     * 更新药品
     * @param updateMedicalDrugInfoDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateMedicalDrugInfo(UpdateMedicalDrugInfoDto updateMedicalDrugInfoDto) {
        return medicalDrugInfoMapper.updateMedicalDrugInfo(updateMedicalDrugInfoDto);
    }


}
